package org.jeecg.modules.backstagehome.controller;

import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.backstagehome.entity.Statistics;
import org.jeecg.modules.backstagehome.entity.UserData;
import org.jeecg.modules.backstagehome.mapper.BackstageHomeMapper;
import org.jeecg.modules.backstagehome.service.IBackstageHomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 后台首页
 *
 * @author guanxiaofeng
 * @date 2020.07.14
 */
@RestController
@RequestMapping("/home")
public class BackstageHomeController {

    @Autowired
    IBackstageHomeService backstageHomeService;

    @Autowired
    BackstageHomeMapper backstageHomeMapper;

    /**
     * 获取用户数据
     *
     * @return 用户统计信息
     */
    @PostMapping(value = "sumNumber")
    public Result sumNumber() {
        Result result = new Result();
        //获取用户总数量
        int sum = backstageHomeService.sumNumber();
        //获取今日新增
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String time = sdf.format(date);
        String time1 = sf.format(date);

        // 存放当前月第一天的日期
        String firstday = "";

        Calendar cal = new GregorianCalendar();
        cal.add(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        // 获取当前月的第一天
        firstday = sdf.format(cal.getTime());
        // 获取当月总增长人数
        int thisMonth = backstageHomeService.thisMonthSum(firstday, time);


        // 获取当前月每天增长的人数
        String a = time.substring(0, 7);
        List<Statistics> statistics = backstageHomeMapper.monthDay(a);

        // 获取当前周每周每天增长人数
        List<Statistics> statisticsWeek = backstageHomeMapper.weekDay(time1);

        // 获取当前天增长人数
        Statistics statisticsDay = backstageHomeMapper.day(time);

        // 获取三天内每天增长的人数
        List<Statistics> statisticsThreeDay = backstageHomeMapper.threeDay(time1);

        // 计算三天用户增长率
        String threeGrowthRate = backstageHomeMapper.getThreeGrowthRate();

        // 计算本周内用户增长率
        String sevenGrowthRate = backstageHomeMapper.getSevenGrowthRate();

        //计算当前月用户增长率
        String monthGrowthRate = backstageHomeMapper.getMonthGrowthRate();

        UserData userData = new UserData();
        userData.setSumNumber(sum);
        // 今日新增人数
        userData.setAddedToday(statisticsDay);
        // 当前月每天的总人数
        userData.setThisMonthSum(statistics);
        // 当前周每天增长人数
        userData.setWeekNum(statisticsWeek);
        // 三天内每天增长的数据
        userData.setThreeDay(statisticsThreeDay);
        // 三天内增长率
        userData.setThreeGrowthRate(threeGrowthRate);
        // 本周内用户增长率
        userData.setSevenGrowthRate(sevenGrowthRate);
        // 当前月用户增长率
        userData.setMonthGrowthRate(monthGrowthRate);
        userData.setThisMonth(thisMonth);
        result.setCode(0);
        result.setResult(userData);
        return result;
    }

    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String time = sdf.format(date);
        System.out.println(time);
        String a = time.substring(0, 7);
        System.out.println(a);
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        calendar.add(calendar.DATE, -1);
        String date2 = sdf.format(calendar.getTime());
        System.out.println(date2);
    }

}
